package com.hpu.tools;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import java.io.File;
import java.util.List;

public class IntentTools {

	public static Intent getWordFileIntent(Context context,String param) {
		Intent intent = null;
		intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		try{
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

				Uri uri = FileProvider.getUriForFile(context, "com.zhuangfei.hpuhelper.fileProvider", new File(param));
				//添加这一句表示对目标应用临时授权该Uri所代表的文件
				intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
				intent.setDataAndType(uri, "application/msword");
			}else {

				Uri uri = Uri.fromFile(new File(param));
				intent.setDataAndType(uri, "application/msword");
			}

			return intent;
		}catch (Exception e){
			return intent;
		}
	}

	public static boolean isIntentAvailable(Context context, Intent intent) {
		final PackageManager packageManager = context.getPackageManager();
		List list = packageManager.queryIntentActivities(intent,
				PackageManager.GET_ACTIVITIES);
		return list.size() > 0;
	}
}
